/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;


import AdminOperations.AdminAnnouncements;
import beans.AnnouncementBean;
import beans.UserType;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Guestannounce;
import xmlMapping.Memberannounce;

/**
 *
 * @author ABDELMOHSEN
 */
public class AnnouncementAction extends org.apache.struts.action.Action {
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserType type=(UserType)request.getSession().getAttribute("type");
        if(type==null||!type.equals(UserType.adm)) return mapping.findForward("login");
        AnnouncementBean bean=(AnnouncementBean)form;
        Date d=new Date();
        int days=0;
        try{
            days=Integer.parseInt(bean.getDays());
        }catch(Exception e){
            return mapping.findForward("done");
        }
        if(days==0||bean.getTitle()==null||bean.getTitle().equals("")||bean.getBody()==null||bean.getBody().equals(""))
            return mapping.findForward("done");
        d.setTime(d.getTime()+(days*24*60*60*1000));
        if(request.getParameter("m")==null){
            Guestannounce announcement=new Guestannounce(bean.getTitle(), bean.getBody(),d);
            AdminAnnouncements model=new AdminAnnouncements();
            model.announceToGuest(announcement);
        }
        else{
            Memberannounce announcement=new Memberannounce(bean.getTitle(), bean.getBody(),d);
            AdminAnnouncements model=new AdminAnnouncements();
            model.announceToMember(announcement);
        }
        return mapping.findForward("done");
    }
}
